package com.kkd.springcloud.controller;

import com.kkd.springcloud.pojo.Dept;
import com.kkd.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 14587
 * @date 2021/1/16 10:52
 */

//提供restful服务
@RestController
public class DeptController {
    @Autowired
    DeptService deptService;
    @Autowired
    DiscoveryClient discoveryClient;

    @PostMapping("/dept/add")
    public int addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept getDept(@PathVariable("id") long id){
        return deptService.queryDeptById(id);
    }

    @RequestMapping("/dept/all")
    public List<Dept> getAll(){
        return deptService.queryDepts();
    }

    @RequestMapping("/dept/discovery")
    public List<ServiceInstance> discover(){
        List<String> services = discoveryClient.getServices();
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+instance.getInstanceId()+instance.getUri());
        }
        return instances;
    }
}
